//
//  NSString+JM.m
//  OpenSesame
//
//  Created by zhangjiaming on 16/1/22.
//  Copyright © 2016年 OpenSesame. All rights reserved.
//

#import "NSString+JM.h"
#import <CommonCrypto/CommonCrypto.h>

@implementation NSString (JM)
/** 字典转成字符串 */
+ (NSString *)stringWithDictionary:(NSDictionary *)dict {
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

+ (BOOL)isPhoneNumber:(NSString *)phoneNumber {
    if (phoneNumber.length != 11)
    {
        return NO;
    }
    /**
     * 手机号码:
     * 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 17[6, 7, 8], 18[0-9], 170[0-9]
     * 移动号段: 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705
     * 联通号段: 130,131,132,155,156,185,186,145,176,1709
     * 电信号段: 133,153,180,181,189,177,1700
     */
    NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|70)\\d{8}$";
    /**
     * 中国移动：China Mobile
     * 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705
     */
    NSString *CM = @"(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\\d{8}$)|(^1705\\d{7}$)";
    /**
     * 中国联通：China Unicom
     * 130,131,132,155,156,185,186,145,176,1709
     */
    NSString *CU = @"(^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\\d{8}$)|(^1709\\d{7}$)";
    /**
     * 中国电信：China Telecom
     * 133,153,180,181,189,177,1700
     */
    NSString *CT = @"(^1(33|53|77|8[019])\\d{8}$)|(^1700\\d{7}$)";
    
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:phoneNumber] == YES)
        || ([regextestcm evaluateWithObject:phoneNumber] == YES)
        || ([regextestct evaluateWithObject:phoneNumber] == YES)
        || ([regextestcu evaluateWithObject:phoneNumber] == YES))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

+ (BOOL)isPassWord:(NSString *)password {
    
    if (password.length  > 16 || password.length < 6) { // 六到十六位
        return NO;
    }
    NSString *regexStr = @"^[a-zA-Z0-9_u4e00-u9fa5]+$";
    NSPredicate *regex = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexStr];
    if ([regex evaluateWithObject:password] == YES) {
        return YES;
    } else {
        return NO;
    }
}

/** 传入一个字符数组，拼接数组中的所有字符 */
+ (NSString *)stringWithStringArray:(NSMutableArray *)strArray {
    NSString *str = @"Method";
    for (int i = 0; i < strArray.count; i++) {
        if (i > 0 && i % 2 != 0) {
            str = [str stringByAppendingString:@"&"];
        }
        if (i % 2 == 0) {
            str =  [str stringByAppendingFormat:@"=%@",strArray[i]];
        } else {
            str =  [str stringByAppendingFormat:@"%@",strArray[i]];
        }
    }
    return str;
}

/** 获取经过MD5加密后的文字 */
- (NSString *)md5String {
    const char* cStr = [self UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(cStr, (unsigned int)strlen(cStr), result);
    NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH];
    for (NSInteger i=0; i<CC_MD5_DIGEST_LENGTH; i++) {
        [ret appendFormat:@"%02x", result[i]];
    }

    return ret;
}

/** 将十六进制字符转化为NSData */
-(NSData*)hexToBytes {
    NSMutableData* data = [NSMutableData data];
    int idx;
    for (idx = 0; idx+2 <= self.length; idx+=2) {
        NSRange range = NSMakeRange(idx, 2);
        NSString* hexStr = [self substringWithRange:range];
        NSScanner* scanner = [NSScanner scannerWithString:hexStr];
        unsigned int intValue;
        [scanner scanHexInt:&intValue];
        [data appendBytes:&intValue length:1];
    }
    return data;
}

/**
 *  删除字符串字符数组给出的特殊字符
 *
 *  @param stringArray 需要删除的特殊字符
 *
 *  @return 删除特殊字符后的字符
 */
- (NSMutableString *)deleateOccurrencesOfStringArray:(NSArray *)stringArray {
    NSMutableString *mutStr = [NSMutableString stringWithString:self];
    for (int i = 0; i < stringArray.count; i++) {
        NSRange range = {0,mutStr.length};
        [mutStr replaceOccurrencesOfString:stringArray[i] withString:@"" options:NSLiteralSearch range:range];
    }
    return mutStr;
}
@end
